<template>
  <div>
    <el-form labelWidth="120px" :disabled="status==1">
      <el-form-item label="请假类型" class="is-required">
        <el-select v-model="leaveData.ltype" placeholder="请选择" style="width:100%">
          <el-option
            v-for="item in leavetypearr"
            :key="item.value"
            :label="item.hash.code_name"
            :value="item.hash.code_id"
          ></el-option>
        </el-select>
        <div class="el-form-item__error" v-if="!$v.leaveData.ltype.required">请选择请假类型</div>
      </el-form-item>
      <gettimelen :lasttimearr.sync="lasttimearr" :timename="'请假时间'" :status="timestatus"></gettimelen>
      <el-form-item label="请假原因" class="is-required">
        <el-input type="textarea" :rows="2" placeholder="请输入内容" v-model="leaveData.leavedetail"></el-input>
        <div class="el-form-item__error" v-if="!$v.leaveData.leavedetail.required">请输入请假原因</div>
      </el-form-item>
      <el-form-item label="请假附件">
        <upload2 :filelimit="3" :filelink.sync="filearr1" :isedit="false" :ispic="false"></upload2>
      </el-form-item>
      <el-form-item label="选择审批流" v-if="status!='1'" class="is-required">
        <choosespl @getsplid="getsplid" :atypeid="'0103'" :num="num" v-if="ischoosespl"></choosespl>
        <div class="el-form-item__error" v-if="!$v.aid.required">请选择审批流</div>
      </el-form-item>
    </el-form>
    <div style="text-align:right" v-if="status==0">
      <el-button size="mini" @click="closedia">取 消</el-button>
      <el-button size="mini" type="primary" @click="submitleave">确 定</el-button>
    </div>
    <div style="text-align:right" v-if="status==2">
      <el-button size="mini" @click="closedia">取 消</el-button>
      <el-button size="mini" type="primary" @click="submitleave2">重新提交</el-button>
    </div>
  </div>
</template>

<script>
import upload2 from '~/components/common/upload2.vue';
import choosespl from '~/components/apply/choosespl.vue';
import gettimelen from '~/components/common/gettimelen.vue';
import { required } from 'vuelidate/lib/validators';

export default {
  validations: {
    aid: {
      required,
    },
    leaveData: {
      ltype: {
        required,
      },
      leavedetail: {
        required,
      },
    },
  },
  components: {
    upload2,
    choosespl,
    gettimelen,
  },
  props: {
    status: Number,
    checkdetail: Object,
    fun: Function,
  },
  data() {
    return {
      timestatus: '',
      flag: false,
      num: 0,
      ischoosespl: false,
      dialogVisible: true,
      ts: this.newStore(),
      lasttimearr: [],
      filearr1: [],
      leavetypearr: [],
      leaveData: {
        starttime: [],
        endtime: [],
        startinterval: [],
        endinterval: [],
        leavenum: '',
        leavedetailnum: [],
      },
      aid: '',
      options: [
        {
          value: '1',
          label: '上午',
        },
        {
          value: '2',
          label: '下午',
        },
      ],
    };
  },
  watch: {
    lasttimearr: {
      handler(newValue) {
        this.num = 0;
        for (let i = 0; i < newValue.length; i += 1) {
          this.num += Number(newValue[i].timelen);
        }
        this.ischoosespl = false;
        this.$nextTick(() => {
          this.ischoosespl = true;
        });
      },
      deep: true,
    },
  },
  created() {
    this.timestatus = this.status;
    this.lasttimearr = [];
    this.filearr1 = [];
    this.getcodeData('0502', '05', 'leavetypearr');
    if (this.status != 0) {
      for (let i = 0; i < this.checkdetail.operation3.length; i += 1) {
        const a = {};
        a.url = this.checkdetail.operation3[i].hash.fileurl;
        a.name = this.checkdetail.operation3[i].hash.filename;
        this.filearr1.push(a);
      }
      for (let i = 0; i < this.checkdetail.operation2.length; i += 1) {
        const b = {};
        b.starttime = this.checkdetail.operation2[i].hash.starttime;
        b.startinterval = this.checkdetail.operation2[i].hash.startinterval;
        b.endtime = this.checkdetail.operation2[i].hash.endtime;
        b.endinterval = this.checkdetail.operation2[i].hash.endinterval;
        b.timelen = this.checkdetail.operation2[i].hash.leavedetailnum;
        this.lasttimearr.push(b);
      }

      this.leaveData = this.checkdetail.operation1[0].hash;

      this.leaveData.starttime = [];
      this.leaveData.endtime = [];
      this.leaveData.startinterval = [];
      this.leaveData.endinterval = [];
      this.leaveData.leavenum = '';
      this.leaveData.leavedetailnum = [];
    }
  },
  methods: {
    closedia() {
      this.$emit('closedia', false);
      this.$router.go(-1);
    },
    checksplnum(money2, splid) {
      const req = {
        handle:
          '9000B8B8D347526960C5BAB1FB6894EF3277431D02D636A5015C1425899A1F59A41CF7A197311E4A',
        flowid: splid,
        number: money2,
      };
      const p = this.spost(this.ts, '/mainservlet', req).then((ts) => {
        if (ts.d.data.operation1[0].hash.cou == '0') {
          this.flag = false;
        } else {
          this.flag = true;
        }
      });
      return p;
    },
    // 重新提交
    submitleave2() {
      if (this.lasttimearr.length == 0) {
        this.$message({
          type: 'success',
          message: '请选择请假时间！',
        });
        return;
      }
      for (let i = 0; i < this.lasttimearr.length; i += 1) {
        if (
          this.lasttimearr[i].starttime == '' ||
          this.lasttimearr[i].starttime == null
        ) {
          this.$message({
            type: 'success',
            message: '请选择开始时间！',
          });
          return;
        }
        if (
          this.lasttimearr[i].startinterval == '' ||
          this.lasttimearr[i].startinterval == null
        ) {
          this.$message({
            type: 'success',
            message: '请选择开始时段！',
          });
          return;
        }
        if (
          this.lasttimearr[i].endtime == '' ||
          this.lasttimearr[i].endtime == null
        ) {
          this.$message({
            type: 'success',
            message: '请选择结束时间！',
          });
          return;
        }
        if (
          this.lasttimearr[i].endinterval == '' ||
          this.lasttimearr[i].endinterval == null
        ) {
          this.$message({
            type: 'success',
            message: '请选择结束时段！',
          });
          return;
        }
        this.leaveData.starttime.push(this.lasttimearr[i].starttime);
        this.leaveData.endtime.push(this.lasttimearr[i].endtime);
        this.leaveData.startinterval.push(this.lasttimearr[i].startinterval);
        this.leaveData.endinterval.push(this.lasttimearr[i].endinterval);
        this.leaveData.leavenum =
          Number(this.leaveData.leavenum) + Number(this.lasttimearr[i].timelen);
        this.leaveData.leavedetailnum.push(this.lasttimearr[i].timelen);
      }
      if (this.$v.$invalid) {
        this.$message({
          type: 'error',
          message: ' 请仔细核对填写信息',
        });
      } else {
        this.checksplnum(this.leaveData.leavenum, this.aid).then(() => {
          if (this.flag == false) {
            this.$message({
              type: 'error',
              message: ' 审批流不符合条件，请重新选择',
            });
            return;
          }
          this.$confirm('确认重新提交请假审批?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          })
            .then(() => {
              const req = {
                handle:
                  '9000B8B8D347526960C5BAB1FB6894EF17427C100F77D58DB382894F762E66D209652941AB86E5AD',
                atype: '0103',
                flowid: this.aid,
                approvalid: this.leaveData.aid,
              };
              this.spost(this.ts, '/mainservlet', req).then((ts) => {
                const req2 = this.leaveData;

                req2.aid = ts.d.data.operation3[0].hash.approvalid;
                req2.handle =
                  'BD6A960AF46EE09F9C5B42D1658B3CCCB16D15507B0143FB64EDEA66339D7026';
                req2.fileurl = this.posturl(this.filearr1);
                req2.filename = this.postname(this.filearr1);

                this.spost(this.ts, '/mainservlet', req2).then(() => {
                  this.$message({
                    type: 'success',
                    message: '重新提交请假审批成功！',
                  });
                  this.fun();
                });
              });
            })
            .catch(() => {
              this.$message({
                type: 'info',
                message: '已取消',
              });
            });
        });
      }
    },
    submitleave() {
      if (this.lasttimearr.length == 0) {
        this.$message({
          type: 'success',
          message: '请选择请假时间！',
        });
        return;
      }
      for (let i = 0; i < this.lasttimearr.length; i += 1) {
        if (
          this.lasttimearr[i].starttime == '' ||
          this.lasttimearr[i].starttime == null
        ) {
          this.$message({
            type: 'success',
            message: '请选择开始时间！',
          });
          return;
        }
        if (
          this.lasttimearr[i].startinterval == '' ||
          this.lasttimearr[i].startinterval == null
        ) {
          this.$message({
            type: 'success',
            message: '请选择开始时段！',
          });
          return;
        }
        if (
          this.lasttimearr[i].endtime == '' ||
          this.lasttimearr[i].endtime == null
        ) {
          this.$message({
            type: 'success',
            message: '请选择结束时间！',
          });
          return;
        }
        if (
          this.lasttimearr[i].endinterval == '' ||
          this.lasttimearr[i].endinterval == null
        ) {
          this.$message({
            type: 'success',
            message: '请选择结束时段！',
          });
          return;
        }
        this.leaveData.starttime.push(this.lasttimearr[i].starttime);
        this.leaveData.endtime.push(this.lasttimearr[i].endtime);
        this.leaveData.startinterval.push(this.lasttimearr[i].startinterval);
        this.leaveData.endinterval.push(this.lasttimearr[i].endinterval);
        this.leaveData.leavenum =
          Number(this.leaveData.leavenum) + Number(this.lasttimearr[i].timelen);
        this.leaveData.leavedetailnum.push(this.lasttimearr[i].timelen);
      }
      if (this.$v.$invalid) {
        this.$message({
          type: 'error',
          message: ' 请仔细核对填写信息',
        });
      } else {
        this.checksplnum(this.leaveData.leavenum, this.aid).then(() => {
          if (this.flag == false) {
            this.$message({
              type: 'error',
              message: ' 审批流不符合条件，请重新选择',
            });
            return;
          }
          this.$confirm('确认提交请假审批?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          })
            .then(() => {
              const req = {
                handle:
                  '9000B8B8D347526960C5BAB1FB6894EF8B7B312373E9BEF47A8517CE4098C19A',
                atype: '0103',
                flowid: this.aid,
              };
              this.spost(this.ts, '/mainservlet', req).then((ts) => {
                const req2 = this.leaveData;

                req2.aid = ts.d.data.operation3[0].hash.approvalid;
                req2.handle =
                  'BD6A960AF46EE09F9C5B42D1658B3CCC60D703BEA30ADC1C';
                req2.fileurl = this.posturl(this.filearr1);
                req2.filename = this.postname(this.filearr1);

                this.spost(this.ts, '/mainservlet', req2).then(() => {
                  this.$message({
                    type: 'success',
                    message: '发起请假审批成功！',
                  });
                  this.$emit('closedia', false);
                });
              });
            })
            .catch(() => {
              this.$message({
                type: 'info',
                message: '已取消',
              });
            });
        });
      }
    },
    getcodeData(supid, codetype, arrname) {
      const req = {
        handle: '67A5B6FEF17D97DECF116BDF95B95C0B167942DD3F1604F9',
        supid,
        codetype,
      };
      this.spost(this.ts, '/mainservlet', req).then((ts) => {
        this[arrname] = ts.d.data.operation1;
      });
    },
    // 公共方法图片数组转路径数组
    posturl(arr) {
      const a = [];
      for (let i = 0; i < arr.length; i += 1) {
        a.push(arr[i].url);
      }
      return a;
    },
    // 公共方法图片数组转名称数组
    postname(arr) {
      const b = [];
      for (let i = 0; i < arr.length; i += 1) {
        b.push(arr[i].name);
      }
      return b;
    },
    getsplid(val) {
      this.aid = val;
    },
  },
};
</script>

<style>
</style>
